package com.eastfair.auth.controller;

import com.eastfair.auth.dto.OrganizationDTO;
import com.eastfair.auth.enumeration.OrganizationOrgTypeEnum;
import com.eastfair.auth.service.OrganizationService;
import com.eastfair.auth.service.ProjectOrganizationService;
import com.eastfair.auth.entity.Organization;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.echo.core.EchoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 * 项目组织controler
 *
 * </p>
 *
 * @author ligang
 * @date 2021-07-16
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/projectOrganization")
@Api(value = "projectOrganization", tags = "")
public class ProjectOrganizationController {

    @Autowired
    private EchoService echoService;

    @Autowired
    ProjectOrganizationService projectOrganizationService;
    @Autowired
    OrganizationService organizationService;

    /**
     * 创建项目
     * @param organizationDTO 组织实体
     * @return
     */
    @ApiOperation(value = "创建项目", notes = "创建项目")
    @PostMapping("/createProject")
    public R<Organization> createProject(@RequestBody @Validated({OrganizationDTO.createBusNode.class }) OrganizationDTO organizationDTO) {
        return projectOrganizationService.createProject(organizationDTO);
    }

    /**
     * 创建项目
     * @param organizationDTO 组织实体
     * @return
     */
    @ApiOperation(value = "修改项目", notes = "修改项目")
    @PostMapping("/updProject")
    public R<Organization> updProject(@RequestBody OrganizationDTO organizationDTO) {
        return projectOrganizationService.updProject(organizationDTO);
    }
    /**
     * 查询项目下的分类组
     * @param organizationTypeEnum 组织类型枚举类
     * @return
     */
    @ApiOperation(value = "查询项目下的分类组", notes = "查询项目下的分类组")
    @GetMapping("/getOrgByType")
    public R<Organization> getOrgByType(OrganizationOrgTypeEnum organizationTypeEnum) {
        if(organizationTypeEnum==null){
            return R.successDef();
        }
        Organization organization = new Organization();
        organization.setOrgType(organizationTypeEnum);
        organization.setProjectId(ContextUtil.getProjectId());
        List<Organization> organizationList =  organizationService.listOfOrganization(organization,null);
        if(organizationList==null || organizationList.isEmpty()){
            return R.successDef();
        }
        return R.success(organizationList.get(0));
    }



}
